﻿//-----------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp
{
    using System;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Models;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views;
    using Windows.ApplicationModel.Activation;
    using Windows.System;
    using Windows.UI.ApplicationSettings;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    public sealed partial class App : Application
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// This is a singleton Application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            Database = new DB();
            Database.LoadDB();
        }

        /// <summary>
        /// Gets the object representation of the application's dataset.
        /// </summary>
        public static DB Database { get; private set; }

        /// <summary>
        /// This function is called when the search charm is activated
        /// </summary>
        /// <param name="args">Search Args Passed</param>
        protected override void OnSearchActivated(SearchActivatedEventArgs args)
        {
            if (Navigation.RootFrame != null)
            {
                var searchPage = Navigation.RootFrame.Content as Search;
                if (searchPage != null)
                {
                    var searchModel = searchPage.DataContext as ViewModels.Search;
                    searchModel.SearchContent(args.QueryText);
                    return;
                }
            }

            this.Launch(typeof(Search), args.QueryText);
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="args">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            this.Launch(typeof(TreasureMapPage));
        }

        /// <summary>
        /// Attempts to launch the page of the specified type.
        /// </summary>
        /// <param name="page">Page attempting to Launch</param>
        /// <param name="parameter">boolean, true to add to back stack.</param>
        private void Launch(Type page, object parameter = null)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            Navigation.RootFrame = rootFrame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Window.Current.Content = rootFrame;
                Navigation.RootFrame = rootFrame;
            }

            if (rootFrame.Content == null || rootFrame.Content.GetType() != page)
            {
                Navigation.Navigate(page, parameter);
            }

            Window.Current.SizeChanged += this.Current_SizeChanged;
            Window.Current.Activate();

            SettingsPane.GetForCurrentView().CommandsRequested += this.App_CommandsRequested;
        }

        /// <summary>
        /// Adds a command to the settings pane.
        /// </summary>
        /// <param name="sender">The settings pane.</param>
        /// <param name="args">The request.</param>
        private void App_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            args.Request.ApplicationCommands.Add(
                new SettingsCommand(
                    "sendfeedback",
                    "Provide Feedback",
                    c =>
                    {
                        ////todo: get a feedback URL
                        var __ = Launcher.LaunchUriAsync(new Uri("mailto:vsartreasuremap@visualstudio.onmicrosoft.com?Subject=Feedback for the ALM Readiness Treasure Map Windows Store Application"));
                    }));

            args.Request.ApplicationCommands.Add(
                new SettingsCommand(
                    "privacyPref",
                    "Privacy Policy",
                    c =>
                    {
                        // Launch the privacy policy URL.
                        var __ = Windows.System.Launcher.LaunchUriAsync(new Uri("http://www.microsoft.com/privacystatement/en-us/core/default.aspx"));
                    }));
        }

        /// <summary>
        /// Deals with resize events
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="e">The parameter is not used.</param>
        private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            Navigation.ReloadCurrentPage();
        }
    }
}
